<?php
namespace Admin\Controller;
use Think\Model;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 支付方式控制器
 * @author James
 */
class SpPayWayController extends AdminController {
    
    //支付方式列表
    public function index(){
        $pid = I('pid');
        if($pid){
            //$map['pid']  =  $pid;
        }else{
            $pid = 0;
            //$map['pid']  =  $pid;
        }
        $name = I('name');
//        $map['status']  =   1;
//        $map['name'] = array('like', '%' . (string) $name . '%');
//        $list = $this->lists('sp_payway', $map);
        
        $Model = new Model();
        $sql = "select a.*,(select GROUP_CONCAT(b.name) from gr_sp_payway b where a.id = b.pid) sname from gr_sp_payway a where status = 1 and pid =".$pid." and a.name like '%".$name."%'";
        $list = $Model -> query($sql);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->assign('pid', $pid);
        $this->meta_title = '数据列表';
        $this->display();
    }
    
    //支付方式添加
    public function add($pid="",$name=""){
        if(IS_POST){
            $payway = array('name' => $name, 'pid' => $pid);
            $id = M('sp_payway')->add($payway);
            if (!$id) {
                $this->error("添加失败！");
            } else {
                $this->success('添加成功！', U('index'));
            }
        }else{
            $this->assign('pid', $pid);
            $this->display();
        }
    }
    
    //支付方式编辑
    public function edit($name="") {
        $id = I('id');
        if (IS_POST) {
            $payway = array('name' => $name);
            if (!M('sp_payway')->where(array('id' => $id))->save($payway)) {
                $this->error("修改失败！");
            } else {
                $this->success('修改成功！', U('index'));
            }
        } else {
            $data = M('sp_payway')->where(array('id' => $id))->find();
            $this->data = $data;
            $this->display();
        }
    }
    
    //支付方式删除
    public function del($id = 0) {
        if ($id) {
            $shop = M("sp_payway");
            
            $data = array('status'=>-1);
            $result = $shop-> where(array('id'=>$id))->setField($data);
            
            if($result){
                $this->success('删除成功！', U('index'));
            }else{
                $this->error("删除失败！");
            }
        }
    }
    
    
    
}
